package cn.team.controller;

import cn.team.Util.MailUtil;
import cn.team.entity.Applay;
import cn.team.service.ApplayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/applay")
public class ApplayController {

    @Resource
    MailSender mailSender;
    @Autowired
    private ApplayService applayService;

    @PostMapping("/insert")
    public int insertApplay(@RequestBody Applay applay) {
        System.out.println(applay);
        Applay applay1 = applayService.selectByOidAndTutor(applay.getOid(),applay.getTutor());
        if (applay1 != null){
            return 0;
        }
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        String msg = "用户:"+applay.getTutor()+"申请了您"+applay.getStartDate()+"至"+applay.getEndDate()+
                "的"+applay.getGrade()+"的"+applay.getSubject()+"的兼职工作";
        MailUtil.sendMailMessage(mailSender,simpleMailMessage,"您有新的兼职申请信息请到程序查看",msg,applay.getParent());
        applayService.insertApplay(applay);
        return 1;
    }

    @DeleteMapping("/deleteByTutorAndId")
    public int deleteByTutorAndId(@RequestParam Integer id, @RequestParam String tutor) {
        return applayService.deleteByTutorAndId(id, tutor);
    }

    @DeleteMapping("/deleteByParentAndId")
    public int deleteByParentAndId(@RequestParam Integer id, @RequestParam String parent) {
        return applayService.deleteByParentAndId(id, parent);
    }

    @GetMapping("/selectByOid")
    public List<Applay> selectByOid(@RequestParam Integer oid) {
        return applayService.selectByOid(oid);
    }

    @GetMapping("/selectByParentAndStartDateAfterNow")
    public List<Applay> selectByParentAndStartDateAfterNow(@RequestParam String parent) {
        return applayService.selectByParentAndStartDateAfterNow(parent);
    }

    @GetMapping("/selectByTutorAndStartDateAfterNow")
    public List<Applay> selectByTutorAndStartDateAfterNow(@RequestParam String tutor) {
        return applayService.selectByTutorAndStartDateAfterNow(tutor);
    }
}